package com.eliota.exception;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.eliota.common.R;
import kotlin.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author M.G
 * @create 2023-03-26 0:11
 * @date 2023/3/26 0:11
 */

/*@ControllerAdvice(basePackages = "com.eliota.controller")*/
@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Log log = LogFactory.get();

    //统一异常处理@ExceptionHandler,主要用于Exception
    //处理CustomException自定义异常
    @ExceptionHandler(CustomException.class)
    @ResponseBody//返回json串
    public R customer(HttpServletRequest request, CustomException e) {
        return R.error(e.getCode(), e.getMsg());
    }

    //统一异常处理@ExceptionHandler,主要用于Exception
    @ExceptionHandler(Exception.class)
    @ResponseBody//返回json串
    public R error(HttpServletRequest request, Exception e) {
        log.error("异常信息：", e);
        return R.error("-1", "系统异常");
    }

}
